﻿using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.APIBase;
using HCQ2_Model.VRDD.OutputDto._20220314;
using HCQ2_Model.VRQDMeta.InputDto._20220418;
using HCQ2_Model.VRQDMeta.OutputDto._20220418;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HCQ2_BLL
{
    public partial class T_VRDDVideoDynamicBLL: IT_VRDDVideoDynamicBLL
    {
        /// <summary>
        ///  获取我的视频动态列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public BasePage<MyVideoNewsOutput> GetMyVideoNews(MyVideoNewsInput param,int create_id, int user_id)
        {
            BasePage<MyVideoNewsOutput> obj = new BasePage<MyVideoNewsOutput>();
            int total;
            var list = DBSession.IT_VRDDVideoDynamicDAL.GetMyVideoNews(param, create_id, user_id, out total);
            list?.ForEach(o =>
            {
                //缩略图
                List<T_DocBagFileRecord> fileImage = DBSession.IT_DocBagFileRecordDAL.Select(s => s.file_id == o.fileID);
                if (fileImage.Count > 0)
                    fileImage.ForEach(s => s.file_url = s.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/"))));
                o.fileImage = fileImage;
                //头像
                if (!string.IsNullOrEmpty(o.ownerImage))
                    o.ownerImage = o.ownerImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            });
            obj.total = total;
            obj.rows = list;
            return obj;
        }
        /// <summary>
        ///  获取动态视频年月
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<List<VideoNewsDateOutput>> GetVideoNewsDateList(VideoNewsDateInput param)
        {
            List<VideoNewsDateOutput> obj = new List<VideoNewsDateOutput>();
            var list = await DBSession.IT_VRDDVideoDynamicDAL.GetVideoNewsDateList(param);
            if (list == null || list.Count <= 0)
                return obj;
            HashSet<int> set = new HashSet<int>(list.Select(s => s.year).ToList());
            //遍历年份
            foreach (var year in set)
            {
                VideoNewsDateOutput temp = new VideoNewsDateOutput { year = year };
                List<VideoNewsMonthList> monthList = new List<VideoNewsMonthList>();
                var mList = list.Where(s => s.year == year).ToList();
                mList.ForEach(o => monthList.Add(new VideoNewsMonthList { month = o.month }));
                temp.monthList = monthList;
                obj.Add(temp);
            }
            return obj;
        }
        /// <summary>
        ///  获取视频动态列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public BasePage<VideoNewsListOutput> GetVideoNews(VideoNewsListInput param, int user_id)
        {
            BasePage<VideoNewsListOutput> obj = new BasePage<VideoNewsListOutput>();
            int total;
            var list = DBSession.IT_VRDDVideoDynamicDAL.GetVideoNews(param, user_id, out total);
            list?.ForEach(o =>
            {
                //缩略图
                List<T_DocBagFileRecord> fileImage = DBSession.IT_DocBagFileRecordDAL.Select(s => s.file_id == o.fileID);
                if (fileImage.Count > 0)
                    fileImage.ForEach(s => s.file_url = s.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/"))));
                o.fileImage = fileImage;
                //头像
                if (!string.IsNullOrEmpty(o.ownerImage))
                    o.ownerImage = o.ownerImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            });
            obj.total = total;
            obj.rows = list;
            return obj;
        }
        /// <summary>
        ///  获取房间商品详情
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public MetaRoomDetailOutput GetMetaRoomDetail(MetaRoomDetailInput param, int user_id)
        {
            var result = DBSession.IT_VRDDVideoDynamicDAL.GetMetaRoomDetail(param,user_id);
            if (result != null)
            {
                //头像
                if(!string.IsNullOrEmpty(result.createImage))
                    result.createImage = result.createImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                //房间图片
                if (string.IsNullOrEmpty(result.imageUrl))
                    result.imageUrl = result.appImage;
                //缩略图
                List<T_DocBagFileRecord> fileImage = DBSession.IT_DocBagFileRecordDAL.Select(s => s.file_id == result.fileID);
                if(fileImage==null || fileImage.Count<=0)
                    fileImage = DBSession.IT_DocBagFileRecordDAL.Select(s => s.file_id == result.appFileID);
                if (fileImage.Count > 0)
                    fileImage.ForEach(s => s.file_url = s.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/"))));
                result.fileImage = fileImage;
            }
            return result;
        }
        /// <summary>
        ///  获取素材商品详情
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public MaterialDetailOutput GetMaterialDetail(MaterialDetailInput param, int user_id)
        {
            var result = DBSession.IT_VRDDVideoDynamicDAL.GetMaterialDetail(param, user_id);
            if (result != null)
            {
                //头像
                if (!string.IsNullOrEmpty(result.createImage))
                    result.createImage = result.createImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                //价格列表
                List<MaterialPriceListModel> listPrice = new List<MaterialPriceListModel>();
                List<T_VRGoodDisiPrice> PriceList = DBSession.IT_VRGoodDisiPriceDAL.Select(s => s.file_id == result.fileID);
                PriceList?.ForEach(s => listPrice.Add(new MaterialPriceListModel { month = s.op_month, name = s.price_name, price = s.ori_price }));
                result.listPrice = listPrice;
            }
            return result;
        }
    }
}